package com.luck.easyfoodbatch.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.mvp.model.MyOrdersM;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.adapter.BaseViewHolder;
import com.luck.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.luck.easyrecyclerview.swipe.MyLayoutManager;

import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class OrderAdapter extends RecyclerArrayAdapter<MyOrdersM.DataBean.ModelsBean> {
    private Context mContext;

    public OnOperationOrder deleteOrder;

    public interface OnOperationOrder {
        void onOperationDelete(int type, MyOrdersM.DataBean.ModelsBean bean);
    }

    public OrderAdapter(Context context, OnOperationOrder deleteOrder) {
        super(context);
        this.mContext = context;
        this.deleteOrder = deleteOrder;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

        return new CouponViewHolder(parent);
    }

    public class CouponViewHolder extends BaseViewHolder<MyOrdersM.DataBean.ModelsBean> {
        RecyclerView recyclerView;
        TextView tv_shop_name, tv_tab_btn_line, tv_status, tv_money, tv_freight;
        TextView tv_cancel, tv_logistics, tv_payment, tv_number;
        RelativeLayout rl_tab_btn;

        public CouponViewHolder(ViewGroup parent) {
            super(parent, R.layout.my_order);
            recyclerView = $(R.id.recyclerView);
            tv_shop_name = $(R.id.tv_shop_name);
            tv_tab_btn_line = $(R.id.tv_tab_btn_line);
            tv_status = $(R.id.tv_status);
            tv_cancel = $(R.id.tv_cancel);
            tv_logistics = $(R.id.tv_logistics);
            tv_payment = $(R.id.tv_payment);
            tv_money = $(R.id.tv_money);
            rl_tab_btn = $(R.id.rl_tab_btn);
            tv_freight = $(R.id.tv_freight);
            tv_number = $(R.id.tv_number);
            MyLayoutManager manager = new MyLayoutManager(mContext);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.addItemDecoration(new RecycleViewDivider(
                    mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), mContext.getResources().getColor(R.color.line_color)));
            recyclerView.setLayoutManager(manager);
        }

        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void setData(final MyOrdersM.DataBean.ModelsBean bean, int position) {

            List<MyOrdersM.DataBean.ModelsBean.OrderInfoBean> list = bean.getOrderInfo();
            OrderItemAdapter adapter = new OrderItemAdapter(mContext, position, list);
            recyclerView.setAdapter(adapter);
            adapter.setOnItemClickListener(new OrderItemAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    mItemClickListener.onItemClick(position, v);
                }
            });
            tv_number.setText(list.size() + "");
            tv_money.setText("￥" + bean.getPay_price());
            tv_freight.setText("(含运费￥" + bean.getPost_price() + ")");

            String shop_name = bean.getShop_name();
            int status = bean.getStatus();
            int comment_status = bean.getComment_status();
            if (!Tools.isNull(shop_name)) {
                tv_shop_name.setText(shop_name);
            } else {
                tv_shop_name.setText("");
            }
            switch (status) {
                case 0:
                    // 待付款 显示去支付、取消订单
                    tv_cancel.setVisibility(View.VISIBLE);
                    tv_cancel.setText("取消订单");
                    tv_logistics.setVisibility(View.GONE);
                    tv_payment.setVisibility(View.VISIBLE);
                    tv_payment.setText("去付款");
                    tv_payment.setTextColor(ContextCompat.getColor(mContext, R.color.tab_color_true));
                    rl_tab_btn.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    //已支付 没有可操作的
                    rl_tab_btn.setVisibility(View.GONE);
                    break;
                case 2:
                    // 已发货
                    tv_cancel.setVisibility(View.GONE);
                    tv_cancel.setText("");
                    tv_logistics.setVisibility(View.VISIBLE);
                    tv_logistics.setText("查看物流");
                    tv_payment.setVisibility(View.VISIBLE);
                    tv_payment.setText("确认收货");
                    tv_payment.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
                    rl_tab_btn.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    // 已收货
                    tv_cancel.setVisibility(View.VISIBLE);
                    tv_logistics.setVisibility(View.GONE);
                    tv_cancel.setText("删除订单");
                    tv_logistics.setText("");
                    if (comment_status == 0) {
                        tv_payment.setText("去评价");
                        tv_payment.setTextColor(ContextCompat.getColor(mContext, R.color.tab_color_true));
                        tv_payment.setVisibility(View.VISIBLE);
                    } else {
                        tv_payment.setVisibility(View.GONE);
                    }
                    rl_tab_btn.setVisibility(View.VISIBLE);
                    break;
                case 17:
                    // 已关闭
                    tv_cancel.setVisibility(View.INVISIBLE);
                    tv_logistics.setVisibility(View.INVISIBLE);
                    tv_payment.setVisibility(View.VISIBLE);
                    tv_payment.setText("删除订单");
                    tv_payment.setTextColor(ContextCompat.getColor(mContext, R.color.color_53));
                    rl_tab_btn.setVisibility(View.VISIBLE);
                    break;
                default:
                    rl_tab_btn.setVisibility(View.GONE);
                    break;
            }
            String orderStatus = subOrderStatus(status);
            tv_status.setText(orderStatus);
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String text = ((TextView) view).getText().toString().trim();
                    if (text.equals("删除订单")) {
                        deleteOrder.onOperationDelete(1, bean);
                    } else if (text.equals("取消订单")) {
                        deleteOrder.onOperationDelete(2, bean);
                    }
                }
            });

            tv_payment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String text = ((TextView) view).getText().toString().trim();
                    if (text.equals("删除订单")) {
                        deleteOrder.onOperationDelete(1, bean);
                    } else if (text.equals("确认收货")) {
                        deleteOrder.onOperationDelete(3, bean);
                    } else if (text.equals("去评价")) {
                        deleteOrder.onOperationDelete(4, bean);
                    } else if (text.equals("去付款")) {
                        deleteOrder.onOperationDelete(6, bean);
                    }
                }
            });

            tv_logistics.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String text = ((TextView) view).getText().toString().trim();
                    if (text.equals("查看物流")) {
                        deleteOrder.onOperationDelete(5, bean);
                    }
                }
            });
        }
    }


    /**
     * 根据订单状态code 返回相应文字显示
     *
     * @return
     */
    public String subOrderStatus(int status) {
        /**
         0=>'未支付',
         1=>'已支付',
         2=>'已发货',
         3=>'已收货',
         4=>'已评价',
         5=>'申请退款退货',//只有发货后才可以申请退款退货
         6=>'同意退款退货',
         7=>'拒绝退款退货',
         8=>'已退款-退款退货',
         9=>'未发货申请退款',
         10=>'未发货同意退款',
         11=>'未发货拒绝退款',
         12=>'已退款-未发货',
         13=>'已发货申请退款',
         14=>'已发货同意退款',
         15=>'已发货拒绝退款',
         16=>'已退款-已发货',
         17=>'已关闭',
         18=>'用户已删除',
         */
        switch (status) {
            case 0:
                return "待付款";
            case 1:
                return "等待卖家发货";
            case 2:
                return "卖家已发货";
            case 3:
                return "已收货";
            case 4:
                return "已评价";
            case 5:
                return "申请退款退货";
            case 6:
                return "同意退款退货";
            case 7:
                return "拒绝退款退货";
            case 8:
                return "已退款-退款退货";
            case 9:
                return "未发货-申请退款";
            case 10:
                return "未发货-同意退款";
            case 11:
                return "未发货-拒绝退款";
            case 12:
                return "已退款-未发货";
            case 13:
                return "已发货-申请退款";
            case 14:
                return "已发货-同意退款";
            case 15:
                return "已发货-拒绝退款";
            case 16:
                return "已退款-已发货";
            case 17:
                return "订单已关闭";
            case 18:
                return "订单已删除";
        }
        return "未知状态";
    }
}
